package com.smsp.controller;


import com.github.pagehelper.PageInfo;
import com.smsp.common.Const;
import com.smsp.common.ServerResponse;

import com.smsp.entity.User;
import com.smsp.service.UserService;

import com.smsp.util.MD5Util;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user/")
public class UserController {

    @Autowired
    private UserService userService;

    public Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping(value = "loginPage.do")
    public String loginPage(){
        return "login";
    }


    /**
     * 请求到首页
     * @return
     */
    @RequestMapping(value = "index.do")
    public String index(){
        return  "index";
    }

    /**
     * 修改密码视图
     * @return
     */
    @RequestMapping(value = "updatePwdPage")
    public String updatePwdPage(){
        return "updatePwdPage";
    }

    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse login(@RequestBody User user, HttpSession session){
        ServerResponse response = userService.login(user.getUsername(),user.getPassword());
        if (response.isSuccess()){
            session.setAttribute("user",response.getData());
        }
        return response;
    }

    @RequestMapping(value = "loginout.do")
    public String loginout(HttpSession session){
        session.invalidate();
        return "login";
    }
    @RequestMapping(value = "userPage.do")
    public String userPage(HttpSession session){
        return "user";
    }

    @RequestMapping(value = "addUserPage.do")
    public String addUserPage(HttpSession session){
        return "addUserPage";
    }

    @RequestMapping(value = "list.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list(){
        ServerResponse serverResponse = userService.selectAll();
        return  serverResponse;
    }


    @RequestMapping(value = "create.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse create(@Validated  @RequestBody User user,BindingResult result){
        if(result.hasErrors()){
            List<ObjectError> allErrors = result.getAllErrors();
            logger.debug(allErrors.get(0).getDefaultMessage(),allErrors);
            return ServerResponse.createByErrorMessage(allErrors.get(0).getDefaultMessage());
        }
        return userService.addUser(user);
    }

    @RequestMapping(value="listPage.do",method=RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllByPage(int page, int pageSize){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PageInfo<User> pager = userService.findMessageDetailByPage(page,pageSize);
        //总条数
        resultMap.put("total", pager.getTotal());
        //获取每页数据
        resultMap.put("rows", pager.getList());
        return resultMap;

    }

    @RequestMapping(value = "frozenUser")
    @ResponseBody
    public ServerResponse frozenUser(Integer id){
        User user = new User();
        user.setId(id);
        user.setUserStatus("1");
       ServerResponse serverResponse = userService.updateUser(user);
       return serverResponse;
    }

    @RequestMapping(value = "cancelFrozenUser")
    @ResponseBody
    public ServerResponse cancelFrozenUser(Integer id){
        User user = new User();
        user.setId(id);
        user.setUserStatus("0");
        ServerResponse serverResponse = userService.updateUser(user);
        return serverResponse;
    }

    @RequestMapping(value="updatePwd.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse updatePwd(@RequestParam String confirmPassword,String password,String newPassword,HttpSession session){

        if(password==""){
            return ServerResponse.createByErrorMessage("密码不能为空");
        }
        if(newPassword==""){
            return ServerResponse.createByErrorMessage("新密码不能为空");
        }
        if(!confirmPassword.equals(newPassword)){
            return ServerResponse.createByErrorMessage("两次密码不一致");
        }

        User user = (User) session.getAttribute("user");
        return userService.updatePwd(user,newPassword);
    }

    @RequestMapping(value="resetPassword.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse resetPassword(@RequestBody User user){
        if(user==null){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        user.setPassword(MD5Util.MD5EncodeUtf8(Const.DEFAULT_PASSWORD));
        return userService.updateUser(user);
    }
}
